﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;
using System.Web.Configuration;
using System.Text;

namespace GiaPhuc
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            Application["CompanyName"] = "TINH TẾ STORE";
            Application["CKeditor:UserFilesPath"] = "../../../../../img/";
            // Code that runs on application startup
            Application["OnlineUsers"] = 0;
            Application["AccessUsers"] = 0;
        }

        void Application_BeginRequest(object sender, EventArgs e)
        {
            //Get the current http context
            HttpContext InRequest = HttpContext.Current;

            //Get the current path
            string OldPath = InRequest.Request.Path.ToLower();

            string[] urls = InRequest.Request.RawUrl.TrimStart('/').Split('/');

            //Check the path whether it is a contextual path
            if (OldPath.EndsWith(".aspx") && urls.Length > 3)
            {
                string Path = urls[0];

                Path = "/" + Path + "/";

                string NewPath = WebConfigurationManager.AppSettings[Path];

                if (null != NewPath)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 1; i < urls.Length - 1; i += 2)
                    {
                        if (i > 1)
                        {
                            stringBuilder.Append("&");
                        }
                        stringBuilder.Append(urls[i]);
                        stringBuilder.Append("=");
                        stringBuilder.Append(urls[i + 1]);
                    }

                    //Rewrite the path with the actual path
                    InRequest.RewritePath(NewPath, String.Empty, stringBuilder.ToString());
                }
            }
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            Application["OnlineUsers"] = null;
            Application["AccessUsers"] = null;
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            Application["OnlineUsers"] = (int)(Application["OnlineUsers"]) + 1;

            string accessUsers;
            using (StreamReader sr = new StreamReader(Server.MapPath("/AccessUsers.nic")))
            {
                accessUsers = sr.ReadToEnd();
                sr.Close();
            }

            if (String.Empty == accessUsers)
                Application["AccessUsers"] = 1;
            else
                Application["AccessUsers"] = int.Parse(accessUsers) + 1;

            using (StreamWriter sw = new StreamWriter(Server.MapPath("/AccessUsers.nic")))
            {
                sw.WriteLine(Application["AccessUsers"].ToString());
                sw.Close();
            }

            Application.UnLock();
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            Application.Lock();
            Application["OnlineUsers"] = (int)(Application["OnlineUsers"]) - 1;
            Application.UnLock();
        }

    }
}
